package logic

import (
	"context"
	"user/models/mysql"
	"user/models/redis"

	"user/internal/svc"
	"user/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCartAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartAddLogic {
	return &CartAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CartAddLogic) CartAdd(in *user.CartAddRequest) (*user.CartAddResponse, error) {
	// todo: add your logic here and delete this line

	g := mysql.Good{}
	err := g.Find(int(in.Id))
	if err != nil {
		logx.Error("商品不存在")
		return nil, err
	}

	if g.Stock <= int(in.Stock) {
		logx.Error("商品库存不足")
		return nil, err
	}

	redis.CartAdd(int(in.UserId), int(in.GoodsId), int(in.Stock), in.Title)

	return &user.CartAddResponse{}, nil
}
